<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends MY_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->model('Member_model','Mmember');
	}
	
	/**
	 * user list
	 * 
	 * @param integer $pindex current page
	 */
	public function index($pindex=1){
		$members = $this->Mmember->get_members();
		var_dump($members);
	}
	
	/**
	 * user login
	 */
	public function login(){
		if ( get_uid()>0 ){
			redirect('/home');
		}
		$this->set_title($this->lang->line('user_login_title'));
		$this->load->library('form_validation');
		$this->form_validation->set_rules('email','lang:user_email','trim|required|valid_email|callback_user_validate');
		$this->form_validation->set_rules('password','lang:user_password','required|trim');
		if ( $this->form_validation->run()===FALSE ) {
			$data['error'] = validation_errors();
			$this->display('login',$data);
		}else{
			$email = $this->input->post('email');
			$password = $this->input->post('password');
			$member = $this->Mmember->get_by('email',$email);
			$userdata = array('uid'=>$member->uid,'nickname'=>$member->nickname,'email'=>$email);
			$this->session->set_userdata($userdata);
			$this->success(site_url('home/index'));
		}
	}
	
	/**
	 * join us
	 */
	public function register(){
		if ( get_uid()>0 ){
			redirect('/home');
		}
		$this->set_title($this->lang->line('user_register_title'));
		$this->load->library('form_validation');
		$this->form_validation->set_rules('email','lang:user_email','required|valid_email|callback_check_email_exitst');
		$this->form_validation->set_rules('nickname','lang:user_register_nickname','required');
		$this->form_validation->set_rules('password','lang:user_password','required');
		$this->form_validation->set_rules('repassword','lang:user_password','required|matches[password]');
		$this->form_validation->set_rules('gender','lang:user_register_gender','required');
		$this->form_validation->set_rules('region','lang:user_register_region','required');
		$this->form_validation->set_rules('birthdate','lang:user_register_birthdate','required');
		
		if ( $this->form_validation->run()===FALSE ) {
			$data['error'] = validation_errors();
			$this->display('register',$data);
		}else{
			$email = $this->input->post('email');
			$password = $this->input->post('password');
			$nickname = $this->input->post('nickname');
			$gender = $this->input->post('gender');
			$region = $this->input->post('region');
			$birthdate = $this->input->post('birthdate');
			$data = array(
					'email'=>$email,
					'password'=>md5($password),
					'nickname'=>$nickname,
					'gender'=>$gender,
					//'region'=>$region,
					'birthdate'=>$birthdate,
					'timestamp'=>time()
					);
			$this->Mmember->insert($data);
			$this->success(site_url('home/index'));
		}
	}
	
	public function user_validate($email){
		$user = $this->Mmember->get_by('email',$email);
		if ( !$user ){
			$this->form_validation->set_message('user_validate','lang:user_does_not_exists');
			return false;
		}
		elseif( $user->status==0 ){
			$this->form_validation->set_message('user_validate','lang:user_does_not_active');
			return false;
		}
		elseif (md5($this->input->post('password')) != $user->password){
			echo md5($this->input->post('password'));
			$this->form_validation->set_message('user_validate','lang:user_password_dose_not_match');
			return false;
		}
		return true;
	}
	
	public function check_email_exitst($email){
		$user = $this->Mmember->get_by('email',$email);
		if ($user) {
			$this->form_validation->set_message('check_email_exitst',$this->lang->line('user_email_have_registed'));
			return false;
		}else{
			return true;
		}
	}
	
	public function crop(){
		if ( get_uid()==0 ){
			redirect('/user/login');
		}
		if ( !$this->is_post() ){
			$this->rm_script('foundation');
			$this->rm_css('foundation');
			$this->add_css('crop.css');
			$this->add_script('imagecropper.min.js');
			$this->add_script('config.js');
			$this->add_script('crop.js');
			$this->display('crop');
		}
		else{
			$uid = get_uid();
			$srcs = $this->input->post('avatar');
			foreach ($srcs as $key=>$src){
				$src = preg_replace('/data:image\/(png|jpg|jpeg|gif|bmp);base64/','',$src);
				$src = base64_decode($src);
				$img = imagecreatefromstring($src);
				$savepath = $this->config->item('avatar_path');
				$name  = $uid.'_'.$key;
				$ext = 'jpg';
				$image_name = $savepath.$name.'.'.$ext;
				imagejpeg($img,  $image_name);
				imagedestroy($img);
			}
			exit;
		}
	}
	

}